﻿using ZYCommon;
using System;
using System.Collections;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TextEditor
{


    public class ZYTextDiv : ZYTextContainer
    {
        private bool bolExpanded = true;
        private Rectangle BoxRect = Rectangle.Empty;
        private System.Drawing.Font myFont = null;
        private string strTitle = null;
        private string strToolTip = null;
        public ZYTextDiv()
        {
            base.ID = StringCommon.AllocObjectName();
        }

        public int MarginLeft
        {
            get
            {
                if (this.timeTitle == "Y")
                    return 0;
                return base.Attributes.GetInt32("marginLeft");

            }
            set
            {
                base.Attributes.SetValue("marginLeft", value);
            }
        }

        public string timeTitle
        {
            get
            {
                return base.Attributes.GetString("timeTitle");

            }
            set
            {
                base.Attributes.SetValue("timeTitle", value);
            }
        }

        public override void AddElementToList(ArrayList myList, bool ResetFlag)
        {
            if (this.bolExpanded)
            {
                if (!this.NoContent || base.myOwnerDocument.Info.DesignMode)
                {
                    base.AddElementToList(myList, ResetFlag);
                }
                else if (myList != null)
                {
                    foreach (ZYTextElement element in base.myChildElements)
                    {
                        if (ResetFlag)
                        {
                            element.Visible = false;
                            element.Index = -1;
                        }
                        if (base.myOwnerDocument.isVisible(element) && (element is ZYTextDiv))
                        {
                            element.Visible = true;
                            (element as ZYTextDiv).AddElementToList(myList, ResetFlag);
                        }
                    }
                }
            }
            else
            {
                myList.Add(this);
            }
        }

        public override Rectangle GetContentBounds()
        {
            this.ResetBoxRect();
            Rectangle rectangle = new Rectangle(this.RealLeft, this.RealTop, base.intClientWidth, this.Height);
            int num = rectangle.Left - this.BoxRect.Left;
            rectangle.X -= num;
            rectangle.Width += num;
            return rectangle;
        }

        public override void GetFinalText(StringBuilder myStr)
        {
            if (!StringCommon.isBlankString(this.Title))
            {
                myStr.Append(this.Title);
                myStr.Append(' ');
            }
            base.GetFinalText(myStr);
        }

        public static ZYTextDiv GetOwnerDiv(ZYTextElement myElement)
        {
            ZYTextDiv div = null;
            while (myElement != null)
            {
                div = myElement as ZYTextDiv;
                if (div == null)
                {
                    myElement = myElement.Parent;
                }
                else
                {
                    return div;
                }
            }
            return div;
        }

        public override bool FromXML(System.Xml.XmlElement myElement)
        {
            //if (myElement.Name == "div"
            //    && myElement.Attributes["title"] != null
            //    && myElement.Attributes["title"].Value.Length > 0
            //    && base.OwnerDocument.Us.InpatientInfo != null)
            //{
            //    string value = myElement.Attributes["title"].Value.Trim();
            //    if (value.Contains("婚育史") && base.OwnerDocument.Us.InpatientInfo.Age <= 14)
            //    {
            //        return false;
            //    }
            //    if (base.OwnerDocument.Us.GetSex == "男" && (value.Contains("月经婚育史") || value.Contains("月经史")))
            //    {
            //        return false;
            //    }
            //    else if ((base.OwnerDocument.Us.GetSex == "女" && this.OwnerDocument.Us.InpatientInfo.Section_Id != 43) && (value == "婚育史:" || value == "婚育史："))
            //    {
            //        return false;
            //    }
            //}
            return base.FromXML(myElement);
        }

        protected override SizeF GetTitleSize()
        {
            string title = this.Title;
            if (this.HideTitle || StringCommon.isBlankString(title))
            {
                return new SizeF(0f, 0f);
            }
            SizeF ef = base.myOwnerDocument.View.Graph.MeasureString(this.Title, this.Font, 1000, StringFormat.GenericDefault);
            ef.Width += 1;
            if (base.TitleLine)
            {
                ef.Width = 0f;
            }
            return ef;
        }

        public SizeF TitleSize()
        {
            string title = this.Title;
            if (this.HideTitle || StringCommon.isBlankString(title))
            {
                return new SizeF(0f, 0f);
            }
            SizeF ef = base.myOwnerDocument.View.Graph.MeasureString(this.Title, this.Font, 1000, StringFormat.GenericDefault);
            ef.Width += 1;
            if (base.TitleLine)
            {
                ef.Width = 0f;
            }
            return ef;
        }

        public override string GetXMLName()
        {
            return "div";
        }

        public override bool HandleClick(int x, int y, MouseButtons Button)
        {
            if (!this.bolExpanded)
            {
                return false;
            }
            return base.HandleClick(x, y, Button);
        }

        public override bool HandleMouseDown(int x, int y, MouseButtons Button)
        {
            //if ((this.BoxRect.Contains(x, y) && (base.myParent != null)) && (base.myLines.Count > 1))
            //{
            //    base.myOwnerDocument.BeginUpdate();
            //    this.bolExpanded = !this.bolExpanded;
            //    this.RefreshSize();
            //    base.myOwnerDocument.RefreshElements();
            //    base.myOwnerDocument.RefreshLine();
            //    base.myOwnerDocument.Content.AutoClearSelection = true;
            //    base.myOwnerDocument.Content.MoveSelectStart(this.bolExpanded ? ((ZYTextElement) base.myChildElements[0]) : ((ZYTextElement) this));
            //    base.myOwnerDocument.EndUpdate();
            //    base.myOwnerDocument.Refresh();
            //    return base.HandleMouseDown(x, y, Button);
            //}
            if (!this.bolExpanded)
            {
                return false;
            }
            return base.HandleMouseDown(x, y, Button);
        }

        public override bool HandleMouseMove(int x, int y, MouseButtons Button)
        {
            if (base.myParent != null)
            {
                if (this.BoxRect.Contains(x, y))
                {
                    base.myOwnerDocument.SetCursor(Cursors.Arrow);
                    base.myOwnerDocument.SetToolTip(this.bolExpanded ? "收缩文本块" : "展开文本块", -1, this.BoxRect);
                    return base.HandleMouseMove(x, y, Button);
                }
                if (!this.bolExpanded && this.myBounds.Contains(x, y))
                {
                    base.myOwnerDocument.SetToolTip(this.strToolTip, -1, base.myBounds);
                }
            }
            return base.HandleMouseMove(x, y, Button);
        }

        public override bool HandleMouseUp(int x, int y, MouseButtons Button)
        {
            return base.HandleMouseUp(x, y, Button);
        }

        public override bool isField()
        {
            return true;
        }

        public override bool isNewLine()
        {
            return true;
        }

        public override bool isNewParagraph()
        {
            return true;
        }

        public override bool isTextElement()
        {
            return !this.bolExpanded;
        }

        public override bool OwnerWholeLine()
        {
            return true;
        }

        public override void RefreshLine()
        {
            this.ResetBoxRect();
            this.myBounds = new Rectangle(this.RealLeft, this.RealTop, this.Width, this.Height);
            if (this.bolExpanded)
            {
                base.RefreshLine();
            }
            else if (base.myParent != null)
            {
                this.strToolTip = base.ToZYString();
                this.strTitle = StringCommon.ClearWhiteSpace(this.strToolTip, 100, true);
                this.strToolTip = StringCommon.ClearBlankLine(this.strToolTip);
                this.strToolTip = this.strToolTip.Replace("\t", "    ");
                if (this.strToolTip.Length > 0x400)
                {
                    this.strToolTip = this.strToolTip.Substring(0, 0x400);
                }
            }
        }

        public override bool RefreshSize()
        {
            return base.RefreshSize();
        }

        public override bool RefreshView()
        {
            if (base.myParent == null)
            {
                base.RefreshView();
                return true;
            }
            int realLeft = this.RealLeft;
            int realTop = this.RealTop;
            this.ResetBoxRect();
            base.RefreshView();
            if (!this.HideTitle && !StringCommon.isBlankString(this.Title))
            {
                SizeF ef = base.myOwnerDocument.View.Graph.MeasureString(this.Title, this.Font, 1000, StringFormat.GenericTypographic);
                int num3 = 0;
                switch (this.TitleAlign)
                {
                    case 0:
                        num3 = 0;
                        break;
                    case 1:
                        num3 = (int)((this.Width - ef.Width) / 2f);
                        break;
                    case 2:
                        num3 = this.Width - ((int)ef.Width) - 10;
                        break;
                }
                if (base.OwnerDocument.EnableJumpPrint)
                {
                    ZYTextElement zl = base.OwnerDocument.JumpElement;
                    if (zl != null)
                    {
                        if (zl.RealTop > realTop)
                        {

                        }
                        else
                        {
                            DrawTitleStr(realLeft, realTop, num3, this.timeTitle);
                        }
                    }
                    else
                    {
                        DrawTitleStr(realLeft, realTop, num3, this.timeTitle);
                    }
                }
                else
                {
                    DrawTitleStr(realLeft, realTop, num3, this.timeTitle);
                }
            }
            //if (this.OwnerDocument.GetInTable(this) != null)
            //{
            //    return true;
            //}
            if (base.myOwnerDocument.Info.ShowExpendHandle)
            {
                if (!base.OwnerDocument.Info.Printing)
                {
                    if (base.myParent == null)
                    {
                        base.myOwnerDocument.View.FillRectangle(Color.Blue, this.BoxRect);
                    }
                    else
                    {
                        base.myOwnerDocument.View.DrawExpendHandle(realLeft, realTop, base.myOwnerDocument.View.DefaultRowHeight(), true);
                    }
                    base.myOwnerDocument.View.DrawLine(SystemColors.WindowText, this.BoxRect.Left + 4, this.BoxRect.Bottom, this.BoxRect.Left + 4, realTop + this.Height);
                    base.myOwnerDocument.View.DrawLine(SystemColors.WindowText, this.BoxRect.Left + 4, realTop + this.Height, this.BoxRect.Right, realTop + this.Height);
                }
            }
            return true;
        }

        void DrawTitleStr(int realLeft, int realTop, int num, string timeTitle)
        {
            string title = this.Title;
            if (timeTitle == "Y")
            {
                string datestring = Bifrost.App.GetTimeString(title);
                base.myOwnerDocument.View.DrawString(datestring, this.Font, SystemColors.WindowText, realLeft + num, realTop);
                string context = title.Substring(datestring.Length, title.Length - datestring.Length).Trim();
                if (!string.IsNullOrEmpty(context.Trim()))
                {
                    SizeF contextWidth = this.OwnerDocument.View.Graph.MeasureString(context, this.Font, 1000, StringFormat.GenericDefault);
                    int leftMargin = realLeft + (this.Width / 2 - (int)(contextWidth.Width / 2));
                    base.myOwnerDocument.View.DrawString(context, this.Font, SystemColors.WindowText, leftMargin, realTop);
                    ZYTextElement lastElement = this.GetLastElement();
                    if (lastElement is ZYTextParagraph)
                    {
                        lastElement.intLeft = leftMargin + (int)contextWidth.Width;
                    }
                }
            }
            else
            {
                base.myOwnerDocument.View.DrawString(title, this.Font, SystemColors.WindowText, realLeft + num, realTop);
            }
        }

        private void ResetBoxRect()
        {
            this.BoxRect = base.myOwnerDocument.View.GetExpendHandleRect(this.RealLeft, this.RealTop, base.myOwnerDocument.View.DefaultRowHeight());
        }

        public bool IsPrintVisible
        {
            get
            {
                return base.Attributes.GetBool("printVisible");
            }
            set
            {
                base.Attributes.SetValue("printVisible", value);
            }
        }

        public override string ToZYString()
        {
            if (!StringCommon.isBlankString(this.Title))
            {
                return (this.Title + base.ToZYString());
            }
            return base.ToZYString();
        }

        public override string ToString()
        {
            return string.Concat(new object[] { "ZYTextDiv Name:", base.myAttributes.GetString("name"), " Childs:", base.myChildElements.Count, " [", this.RealLeft, "-:", this.RealTop, " ", this.Width, "-", this.Height });
        }

        public bool Expanded
        {
            get
            {
                return this.bolExpanded;
            }
            set
            {
                if (this.bolExpanded != value)
                {
                    this.bolExpanded = value;
                    if (!this.bolExpanded)
                    {
                        this.strTitle = this.ToZYString();
                    }
                }
            }
        }

        public override System.Drawing.Font Font
        {
            get
            {
                if (this.myFont == null)
                {
                    this.myFont = base.myOwnerDocument.View._CreateFont(this.FontName, this.FontSize, this.FontBold, this.FontItalic, this.FontUnderLine);
                }
                return this.myFont;
            }
            set
            {
                this.FontName = value.Name;
                this.FontSize = value.Size;
                this.FontBold = value.Bold;
                this.FontItalic = value.Italic;
                this.FontUnderLine = value.Underline;
            }
        }

        public override bool FontBold
        {
            get
            {
                return true;
            }
        }

        public override bool FontItalic
        {
            get
            {
                return base.Attributes.GetBool("fontitalic");
            }
            set
            {
                base.Attributes.SetValue("fontitalic", value);
                this.myFont = null;
            }
        }

        public override string FontName
        {
            get
            {
                return base.Attributes.GetString("fontname");
            }
            set
            {
                base.Attributes.SetValue("fontname", value);
                this.myFont = null;
            }
        }

        public override float FontSize
        {
            get
            {
                if (base.Attributes.Contains("fontsize"))
                {
                    return base.myAttributes.GetFloat("fontsize");
                }
                else
                {
                    return base.OwnerDocument.Info.defaultFontSize;
                }
            }
            set
            {
                base.Attributes.SetValue("fontsize", value);
                this.myFont = null;
            }
        }

        public override bool FontUnderLine
        {
            get
            {
                return base.Attributes.GetBool("fontunderline");
            }
            set
            {
                base.Attributes.SetValue("fontunderline", value);
                this.myFont = null;
            }
        }

        public override int Height
        {
            get
            {
                if (this.bolExpanded)
                {
                    return base.Height;
                }
                return base.myOwnerDocument.View.DefaultRowHeight();
            }
        }

        public bool HideTitle
        {
            get
            {
                return base.myAttributes.GetBool("hidetitle");
            }
            set
            {
                base.myAttributes.SetValue("hidetitle", value);
            }
        }

        public override int Left
        {
            get
            {
                return MarginLeft;
            }
            set
            {
                if (base.myParent == null)
                {
                    base.intLeft = value;
                }
                else
                {
                    base.intLeft = MarginLeft;
                }
            }
        }

        public override bool Locked
        {
            get
            {
                if (base.myOwnerDocument.Loading)
                {
                    return false;
                }
                return (base.myOwnerDocument.Locked || (this.NoContent && !base.myOwnerDocument.Info.DesignMode));
            }
            set
            {
                base.Locked = value;
            }
        }

        public bool NoContent
        {
            get
            {
                return base.myAttributes.GetBool("nocontent");
            }
            set
            {
                base.myAttributes.SetValue("nocontent", value);
            }
        }

        public string Title
        {
            get
            {
                return base.myAttributes.GetString("title");
            }
            set
            {
                base.myAttributes.SetValue("title", value);
            }
        }

        public int TitleAlign
        {
            get
            {
                return base.myAttributes.GetInt32("align");
            }
            set
            {
                base.myAttributes.SetValue("align", value);
            }
        }

        public bool TitleCenterAlign
        {
            get
            {
                return (this.TitleAlign == 1);
            }
            set
            {
                this.TitleAlign = 1;
            }
        }

        public bool TitleLeftAlign
        {
            get
            {
                return (this.TitleAlign == 0);
            }
            set
            {
                this.TitleAlign = 0;
            }
        }

        public new bool Visible
        {
            get
            {
                return base.myAttributes.GetBool("visible");
            }
            set
            {
                base.myAttributes.SetValue("visible", value);
            }
        }

        public bool TitleRightAlign
        {
            get
            {
                return (this.TitleAlign == 2);
            }
            set
            {
                this.TitleAlign = 2;
            }
        }

        public override int Width
        {
            get
            {
                if (base.myParent == null)
                {
                    return base.myOwnerDocument.Pages.StandardWidth;
                }
                return (base.myParent.Width - this.Left);
            }
            set
            {
                base.Width = value;
            }
        }
    }
}

